package com.qfedu.mtl.dao.impl;

import com.qfedu.mtl.dao.WebUserServiceDao;
import com.qfedu.mtl.entity.Webuser;
import com.qfedu.mtl.query.WebUserQuery;
import com.qfedu.mtl.util.DBUtil;
import com.qfedu.mtl.util.StrUtil;
import com.qfedu.mtl.util.handler.impl.BeanHandler;
import com.qfedu.mtl.util.handler.impl.BeanListHandler;
import com.qfedu.mtl.util.handler.impl.NumberHandler;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @version v1.0
 * @Date 2023/10/3 17:35
 * @Author Kyi
 * @Email zywKyi@163.com
 */
public class WebUserServiceDaoImpl implements WebUserServiceDao {
//    uid ,uname,uavatar ,uphone,ugender ,ip ,time

    @Override
    public void save(Webuser webuser) {
        String sql ="insert into tb_webuser(uname,uavatar ,uphone,ugender ,ip ,time,email) value(?,?,?,?,?,?,?)";
        DBUtil.executeDML(sql,DBUtil.getConn(),webuser.getUname(),webuser.getUavatar(),webuser.getUphone(),
                                        webuser.getUgender(), webuser.getIp(),new Date(),webuser.getEmail());

    }

    @Override
    public void remove(Integer id) {
        String sql ="DELETE from tb_webuser WHERE uid=?";
        DBUtil.executeDML(sql,DBUtil.getConn(),id);
    }



    @Override
    public void update(Webuser wu) {
        String sql = "update tb_webuser set uname=?,uavatar=? ,uphone=?,ugender=? ,ip=? ,email=?  where uid=?";
        DBUtil.executeDML(sql,DBUtil.getConn(),wu.getUname(),wu.getUavatar(),wu.getUphone(),wu.getIp(),new Date(),wu.getEmail(),wu.getUid());


    }

    @Override
    public Webuser query(Integer id) {
        String sql ="select * from tb_webuser where uid=?";
        Webuser webuser = DBUtil.executeDQL(sql, DBUtil.getConn(), new BeanHandler<>(Webuser.class, true), id);
        return webuser;
    }


    @Override
    public List<Webuser> findAll(Integer pageNum, Integer pageSize, WebUserQuery webUserQuery) {
        StringBuilder sql = new StringBuilder("select * from tb_webuser where 1=1 ");
        List<Object> list = new ArrayList<>();
        if (!StrUtil.isNullOrEmpty(webUserQuery.getUname())){
            sql.append(" and  uname like concat('%',?,'%') ");
            list.add(webUserQuery.getUname());
        }
        if (!StrUtil.isNullOrEmpty(webUserQuery.getEmail())){
            sql.append(" and email like concat('%',?,'%') ");
            list.add(webUserQuery.getEmail());
        }
        if (webUserQuery.getUgender()!=null){
            sql.append("   and ugender=? ");
            list.add(webUserQuery.getUgender());
        }
        if (webUserQuery.getUstate()!=null){
            sql.append("   and ustate=? ");
            list.add(webUserQuery.getUstate());
        }
        sql.append("  limit ?,?  ");
        list.add((pageNum-1)*pageSize);
        list.add(pageSize);
        List<Webuser> webuserList = DBUtil.executeDQL(sql.toString(), DBUtil.getConn(), new BeanListHandler<>(Webuser.class, true), list.toArray());
        return webuserList;
    }

    @Override
    public Integer findcount(WebUserQuery webUserQuery) {

        StringBuilder sql = new StringBuilder("select count(*) from tb_webuser where 1=1 ");
        List<Object> list = new ArrayList<>();
        if (!StrUtil.isNullOrEmpty(webUserQuery.getUname())){
            sql.append(" and uname like concat('%',?,'%') ");
            list.add(webUserQuery.getUname());
        }
        if (!StrUtil.isNullOrEmpty(webUserQuery.getEmail())){
            sql.append(" and email like concat('%',?,'%') ");
            list.add(webUserQuery.getEmail());
        }
        if (webUserQuery.getUgender()!=null){
            sql.append("   and ugender=? ");
            list.add(webUserQuery.getUgender());
        }

        Long aLong = DBUtil.executeDQL(sql.toString(), DBUtil.getConn(), new NumberHandler<>(Long.class), list.toArray());
        return aLong.intValue();
    }
}
